import {Component, EventEmitter, Input, OnChanges, OnInit, Output, SimpleChanges} from '@angular/core';
import {ApiUrl} from "../../../config/api-url";
import {BaseService} from "../../../shared/service/base.service";

@Component({
  selector: 'app-device-set',
  templateUrl: './device-set.component.html',
  styleUrls: ['./device-set.component.less']
})
export class DeviceSetComponent implements OnInit, OnChanges {


  @Input()
  isVisible = false;

  @Output()
  emitVisible: EventEmitter<any> = new EventEmitter();
  isOkLoading: any;
  isSpinning: any;

  @Input()
  inputDevice: any;

  baudId: any;
  dataBit: any;
  parityBit: any;
  stopBit: any;
  pulseSwitchState: any;
  // 0:常开，1：常闭
  level: any;

  // 波特率集合
  baudList: any

  constructor(private baseService: BaseService) { }

  ngOnInit(): void {
    this.baudList = new Array();
    this.findAllBaud();
  }

  handleCancel() {
    this.isSpinning = false;
    this.isVisible = false;
    this.emitVisible.emit(this.isVisible);
  }

  handleOk() {
    this.isSpinning = true;
    if (this.baudId == null || this.baudId == ""){
      this.isSpinning = false;
      this.baseService.layer.messageError('请选择波特率！');
      return;
    }
    if (this.dataBit == null || this.dataBit == ""){
      this.isSpinning = false;
      this.baseService.layer.messageError('请选择数据位！');
      return;
    }
    if (this.parityBit == null || this.parityBit == ""){
      this.isSpinning = false;
      this.baseService.layer.messageError('请选择校验位！');
      return;
    }
    if (this.stopBit == null || this.stopBit == ""){
      this.isSpinning = false;
      this.baseService.layer.messageError('请选择停止位！');
      return;
    }
    if (this.pulseSwitchState == null){
      this.isSpinning = false;
      this.baseService.layer.messageError('请选择脉冲开关！');
      return;
    }
    if (this.level == null){
      this.isSpinning = false;
      this.baseService.layer.messageError('请选择常开、常闭！');
      return;
    }

    let body = {
      id: this.inputDevice.id,
      baudId: this.baudId,
      dataBit:this.dataBit,
      parityBit:this.parityBit,
      stopBit:this.stopBit,
      pulseSwitchState:this.pulseSwitchState == 1 ? true : false,
      level:this.level ? 0 : 1,
    };

    console.info(body)
    this.baseService.put(ApiUrl.DeviceUrl + '/sendUpdateBoxConfigById', body).subscribe(
      data => {
        this.isSpinning = false;
        if (data.status == 0) {
          this.isVisible = false;
          this.emitVisible.emit(this.isVisible);
          this.baseService.layer.messageSuccess('发送成功，预计2分钟内生效！');
        } else {
          this.baseService.layer.messageError('发送失败：' + data.msg);
        }
      }
    );
  }

  findAllBaud(){
    this.baseService.get(ApiUrl.BaudUrl + '/findAllBaud', null).subscribe(
      data => {
        if (data.status == 0) {
          this.baudList = data.data;
        } else {
          this.baseService.layer.messageError('获取全部波特率失败：' + data.msg);
        }
      }
    );
  }

  ngOnChanges(changes: SimpleChanges): void {
    try {
      this.baudId = this.inputDevice.baudId;
      this.dataBit = this.inputDevice.dataBit + "";
      this.parityBit = this.inputDevice.parityBit + "";
      this.stopBit = this.inputDevice.stopBit + "";
      this.pulseSwitchState = this.inputDevice.pulseSwitchState == 1 ? true : false;
      this.level = this.inputDevice.level == 1 ? false : true;
    }catch (e) {
      console.log(e.msg);
    }
  }
}
